using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TermitesMagnets
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Pwu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        //private Boolean isEnabled;
        public Boolean isEnabled;
        private Boolean isDisplay;
        private Boolean isCaught;
        private int counter;
        private Color c = Color.Purple;
        public Boolean IsEnabled
        {
            get { return isEnabled; }
            set { isEnabled = value; }
        }

        

        public Boolean IsDisplay
        {
            get { return isDisplay; }
            set { isDisplay = value; }
        }

        public Boolean IsCaught
        {
            get { return isCaught; }
            set { isCaught = value; }
        }

        private GameplayScreen gamePlayScreen;
        private static int widthP;
        private static int heightP;
        private static Texture2D texture;
        private bool selected = false;
        protected Level parent;

        private Vector2 position;
        private Vector2 speed;
        private static SoundEffect humanSound;
        private static SoundEffect aiSound;


        public Level Parent{ get { return parent; }}
        public bool Selected{ get { return selected; } set { selected = value; }}
        public Vector2 Position{ get { return position; } set { position = value; }}      
        public Vector2 Speed{ get { return speed; } set { speed = value; }}

        public Pwu(Game game, Level parent, Vector2 position)
            : base(game)
        {
            this.isEnabled = false;
            this.gamePlayScreen = GameplayScreen.GlobalGame;
            this.parent = parent;
            this.speed = new Vector2(0, 0);
            this.position = position;
            this.parent.AddComponent(this);
            
        }

        public Pwu(Game game, Level parent, Vector2 position, int P)
            : this(game, parent, position)
        {

        }

        public static void LoadContent(ContentManager Content)
        {
            texture = Content.Load<Texture2D>(@"Images\new_pwu");
            humanSound = Content.Load<SoundEffect>(@"Sounds\Godlike");
            aiSound = Content.Load<SoundEffect>(@"Sounds\Humiliation");
            heightP = texture.Height;
            widthP = texture.Width;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {


            #region PwuInteractions

            if (((int)GameplayScreen.seconds % 15 == 0) &&
                (!this.isEnabled))
            {
                this.Position = new Vector2(Game1.Rand.Next(parent.Board.Cols), Game1.Rand.Next(parent.Board.Rows));
                    this.isEnabled = !this.isEnabled;
            }
            #endregion

            if (isEnabled==false)
                    { isDisplay = false; }
            else
                    { isDisplay = true; }
            if (isDisplay)
            {
                this.Interact();
                if (isCaught) 
                {
                    isEnabled = false; isCaught = false; isDisplay = false;

                    this.position = new Vector2(Game1.Rand.Next(parent.Board.Cols), Game1.Rand.Next(parent.Board.Rows));

                }
            }  

            //change seen and unseen
            //if caugth change to seen

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            gamePlayScreen.SpriteBatch.Begin();
            Texture2D current;
            if (isDisplay)
            {
                current = texture;
                Vector2 worldPosition = gamePlayScreen.CurrentLevel.Board.WorldPositionToReal(position);
                Rectangle destRect = new Rectangle((int)worldPosition.X, (int)worldPosition.Y, Pwu.widthP, Pwu.heightP);
                Vector2 origin = new Vector2(Pwu.widthP / 2, Pwu.heightP / 2);
                gamePlayScreen.SpriteBatch.Draw(current,
                                      destRect,
                                      null,
                                      Color.White,//Color.Purple,     //change!!
                                      0, // rotation
                                      origin,
                                      new SpriteEffects(),
                                      0);
            }
            gamePlayScreen.SpriteBatch.End();
            base.Draw(gameTime);
        }

        public virtual void Interact()
        {
            if (isEnabled)
            {
                
                foreach (Magnet mag in parent.Magnets)
                {
                    if (
                        ((mag.Position.X - this.position.X) >= -widthP / 2) & ((mag.Position.X - this.position.X) <= widthP / 2)
                        &
                        ((mag.Position.Y - this.position.Y) >= -heightP / 2) & ((mag.Position.Y - this.position.Y) <= heightP / 2)
                    )
                    {
                        if (mag.IsAI)
                        {
                            GameplayScreen.PlaySound(aiSound);
                        }
                        else
                        {
                            GameplayScreen.PlaySound(humanSound);
                        }
                       isCaught = true;
                       DoSomething(mag); 
                    break;
                    }
                }
            }
        }
        public virtual void DoSomething(Magnet mag) 
        {

        }


    }
}
